import javax.imageio.stream.ImageInputStream
import javax.imageio.ImageReader
import javax.imageio.ImageIO

class PhotoController {

    def index = {redirect(action: list, params: params)}

    // the delete, save and update actions only accept POST requests
    def allowedMethods = [delete: 'POST', save: 'POST', update: 'POST']

    def list = {
        if (!params.max) params.max = 10
        [photoList: Photo.list(params)]
    }

    def show = {
        def photo = Photo.get(params.id)

        if (!photo) {
            flash.message = "Photo not found with id ${params.id}"
            redirect(action: list)
        }
        else {return [photo: photo]}
    }

    def delete = {
        def photo = Photo.get(params.id)
        if (photo) {
            photo.delete()
            flash.message = "Photo ${params.id} deleted"
            redirect(action: list)
        }
        else {
            flash.message = "Photo not found with id ${params.id}"
            redirect(action: list)
        }
    }

    def edit = {
        def photo = Photo.get(params.id)

        if (!photo) {
            flash.message = "Photo not found with id ${params.id}"
            redirect(action: list)
        }
        else {
            return [photo: photo]
        }
    }

    def update = {
        def photo = Photo.get(params.id)
        if (photo) {
            photo.properties = params
            if (!photo.hasErrors() && photo.save()) {
                flash.message = "Photo ${params.id} updated"
                redirect(action: show, id: photo.id)
            }
            else {
                render(view: 'edit', model: [photo: photo])
            }
        }
        else {
            flash.message = "Photo not found with id ${params.id}"
            redirect(action: edit, id: params.id)
        }
    }

    def create = {
        def photo = new Photo()
        photo.properties = params
        return ['photo': photo]
    }

    def save = {
        def photo = new Photo(params)
        if (!photo.hasErrors() && photo.save()) {
            flash.message = "Photo ${photo.id} created"
            redirect(action: show, id: photo.id)
        }
        else {
            render(view: 'create', model: [photo: photo])
        }
    }


    def photo = {
        if (params.id) {
            def photo = Photo.get(params.id)
            response.setContentType(
                    getFormatName(new ByteArrayInputStream(photo.photo)))
            //response.setContentType("image/jpeg")
            return response.outputStream << photo.photo;
        }
    }

    def getFormatName = {o ->
        try
        {
            // Create an image input stream on the image
            ImageInputStream iis = ImageIO.createImageInputStream(o);

            // Find all image readers that recognize the image format
            Iterator<ImageReader> iter = ImageIO.getImageReaders(iis);
            if (!iter.hasNext())
            {
                // No readers found
                return null;
            }

            // Use the first reader
            ImageReader reader = iter.next();

            // Close stream
            iis.close();

            // Return the format name
            return reader.getFormatName();
        }
        catch (IOException e)
        {
            //
        }
        return null;
    }

}
